/* global emptyFunc */

/**
 * 获取表单数据
 * @param {type} $form
 * @returns {unresolved}
 */
function getFormData($form) {
    if (!$form || $form.length == 0) {
        $form = $('#myForm');
    }
    var fromData = $form.serializeArray();
    var data = {};
    $.each(fromData, function () {
        var name = this.name;
        var value = $.trim(this.value);
        data[name] = value;
    });
    return data;
}

/**
 * api调用
 * @param url
 * @param data
 * @param callback
 * @param loading
 */
function sApi(url, data, callback, loading) {
    if (typeof callback !== "function") {
        callback = emptyFunc;
    }

    if (typeof loading == "undefined") {
        loading = true;
    }

    var param = {
        "url": url,
        "data": data,
        "success": function (res) {
            if (!res) {
                res = errRes("网络响应超时");
            }
            callback(res);
        },
        "error": function () {
            callback(errRes("网络响应超时"));
        },
        "loading": loading ? true : false
    };
    $.sAjax(param);
}